package com.shop.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.Order;
import com.shop.feign.ElasticsearchFeign;
import com.shop.mapper.MerchantOrderMapper;
import com.shop.model.PageParam;
import com.shop.model.vo.ESPageInfo;
import com.shop.model.vo.ElasticSearchLog;
import com.shop.model.vo.Rsp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class MerchantOrderService {

    @Autowired
    MerchantOrderMapper merchantOrderMapper;

    @Autowired
    ElasticsearchFeign elasticsearchFeign;


    public PageInfo<Order> getOrderListByMerchantId(Integer merchantId, PageParam<Order> pageParam) {

        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<Order> orderList = merchantOrderMapper.getOrderListByMerchantId(merchantId);
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);

        return orderList.size() > 0 ? pageInfo : null;
    }

    public int updateOrderStatus(Order order) {
        return merchantOrderMapper.updateOne(order);
    }

    public Map<String, Object> getTotalSalePriceByMerchantId(Integer merchantId) {

        List<Order> orderList = merchantOrderMapper.getTotalSalePriceByMerchantId(merchantId);

        System.out.println(orderList.toString());

        Double totalSalePrice = 0d;

        List<String> listDate = new ArrayList();

        List<Integer> listOrderCount = new ArrayList<>();

        List<Double> listOrderPrice = new ArrayList<>();

        Map<String, Object> map = new HashMap<>(16);
        Map<String, Double> map2 = new HashMap<>(16);

        for (int i = 0; i < orderList.size(); i++) {
            totalSalePrice = orderList.get(i).getTotalPrice() + totalSalePrice;
            int dayNum = orderList.get(i).getCreateTime().getDate();
            int monthNum = orderList.get(i).getCreateTime().getMonth();
            String date = monthNum + "-" + dayNum;
            listDate.add(date);
            Double salePrice = 0d;
            int count = 0;
            for (int j = i; j < orderList.size(); j++) {
                if (orderList.get(i).getCreateTime().getDate() == orderList.get(j).getCreateTime().getDate()) {
                    salePrice = salePrice + orderList.get(j).getTotalPrice();
                    count++;
                }
            }
            if (map2.get(date) == null) {
                map2.put(date, salePrice);
                listOrderPrice.add(salePrice);
            }
            if (map.get(date) == null) {
                map.put(date, count);
                listOrderCount.add(count);
            }
        }


        //System.out.println(map.toString());

        // System.out.println(map2.toString());
//
//        for (int i = 0; i < list.size(); i++) {
//            int count = 0;
//            for (int j = i; j < list.size(); j++) {
//                if (list.get(i).equals(list.get(j))) {
//                    count++;
//                }
//            }
//            if (map.get(list.get(i)) == null) {
//                map.put(list.get(i), count);
//                list2.add(count);
//            } else {
//                continue;
//            }
//        }

        listDate = listDate.stream().distinct().collect(Collectors.toList());

        map.put("dateArray", listDate);
        map.put("priceArray", listOrderPrice);

        List<Double> randompriceArray = new ArrayList<>(listOrderPrice);
        Collections.shuffle(randompriceArray);
        map.put("randompriceArray", randompriceArray);


        map.put("countArray", listOrderCount);
        List<Integer> randomCountArray = new ArrayList<>(listOrderCount);
        Collections.shuffle(randomCountArray);
        map.put("randomCountArray", randomCountArray);

        map.put("totalSalePrice", totalSalePrice);
        map.put("totalSaleCount", orderList.size());
        map.put("orderList", orderList);
        // System.out.println(map.toString());


        PageParam<ElasticSearchLog> pageParam = new PageParam<>();
        pageParam.setPageNum(1);
        pageParam.setPageSize(5);
        pageParam.setSuperSearchKeyWord("/category/getByMerchantId");
        Rsp log = elasticsearchFeign.getLogPV(pageParam);


        String str = elasticsearchFeign.getLog(pageParam).toString();
       //System.out.println(str);


        String str1 = JSON.toJSONString(log.getContent());

        JSONObject jsonObject = JSONObject.parseObject(str1);

        String list = jsonObject.getString("list");

        List<ElasticSearchLog> elasticSearchLogs = JSON.parseArray(list, ElasticSearchLog.class);


       // System.out.println(elasticSearchLogs.toString());
//        List<ElasticSearchLog> elasticSearchLogs= JSON.parseArray(map3.get("list").toString(), ElasticSearchLog.class);
//
//        System.out.println(elasticSearchLogs.toString());
//
        Map<String, Integer> map3 = new HashMap<>();

        List<Integer> listPV=new ArrayList<>();

        int totalPV = 0;
        for (ElasticSearchLog elasticSearchLog : elasticSearchLogs) {

            //System.out.println(elasticSearchLog.getTime().getDate());
            String date = elasticSearchLog.getTime().getMonth() + "-" + elasticSearchLog.getTime().getDate();
            int length = elasticSearchLog.getRequestData().getBytes().length;
            if (elasticSearchLog.getRequestData().substring(4, length - 2).equals(merchantId.toString())) {
                totalPV++;
            }
            int datePV = 0;
            for (ElasticSearchLog elasticSearchLog2 : elasticSearchLogs) {
                if (elasticSearchLog.getTime().getDate() == elasticSearchLog2.getTime().getDate()) {
                    datePV++;
                }
            }
            if (map3.get(date) == null) {
                map3.put(date, datePV);
                listPV.add(datePV);
            }
        }

        System.out.println(map3.toString());
        System.out.println(listPV.toString());

        map.put("totalPV",totalPV);
        map.put("listPV",listPV);

        List<Integer> randomListPV=new ArrayList<>(listPV);
        Collections.shuffle(randomListPV);

        map.put("randomListPV",randomListPV);

        return map;
    }
}
